//
//  AccountPriceView.m
//  NtoccShipper
//
//  Created by zcj on 2021/1/15.
//  Copyright © 2021 zhuchongjin. All rights reserved.
//

#import "AccountPriceSureView.h"

@interface AccountPriceSureView()<UITextFieldDelegate>
@property (nonatomic,copy) OnSureClick sureBlock;
@property (nonatomic,copy) OnCancleClick cancleBlock;
@property (nonatomic,strong) FlexFrameView *frameView;
@property (nonatomic,strong) CJLabel *labDanJia;
@property (nonatomic,strong) CJLabel *labDanJiaTitle;
@property (nonatomic,strong) CJLabel *labelDaiShouRen;
@property (nonatomic,strong) CJLabel *labelDaiShouRenVlue;
@property (nonatomic,strong) CJLabel *labZhuangChe;
@property (nonatomic,strong) CJLabel *labZhuangCheTitle;
@property (nonatomic,strong) CJLabel *labXieChe;
@property (nonatomic,strong) CJLabel *labXieCheTitle;
@property (nonatomic,strong) CJLabel *labOtherFeiYong;
@property (nonatomic,strong) CJLabel *labQiTaFeiYong;
@property (nonatomic,strong) CJLabel *labYiChangFei;
@property (nonatomic,strong) CJLabel *labYuFuXianJin;
@property (nonatomic,strong) CJLabel *labYuFuYouKa;
@property (nonatomic,strong) CJLabel *labBaoXianFei;
@property (nonatomic,strong) CJTextField *fieldsSiJiShiSou;
@property (nonatomic,strong) CJLabel *labShiJiJieSuan;
@property (nonatomic,strong) CJLabel *labShiFuJin;

@end

@implementation AccountPriceSureView

+ (AccountPriceSureView *)shared {

    static dispatch_once_t predicate;

    static AccountPriceSureView *objc;

    dispatch_once(&predicate, ^{

        objc = [[AccountPriceSureView alloc] init];

    });

    return objc;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithListContent:(NSMutableArray * __nullable)arrContent index:(int)index ClickOnSure:(OnSureClick )sureClick ClickOnCancle:(OnCancleClick )cancleClick{
    
//    if (self = [super init]) {
        
        self.sureBlock = sureClick;
        self.cancleBlock = cancleClick;
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.backgroundColor = [[UIColor cjBlackColor] colorWithAlphaComponent:0.3];

        CGRect rcFrame = CGRectMake(0, 0, kScreenWidth, 0);

        if (!self.frameView) {
            self.frameView = [[FlexFrameView alloc] initWithFlex:@"AccountPriceSureView" Frame:rcFrame Owner:self];
        }
        self.frameView.flexibleHeight = YES;
        // 带收款人
        NSMutableDictionary *dicMuta4 = arrContent[6 + index];
        self.labelDaiShouRen.text = [CJControl getThreeUnaryValueStr:dicMuta4[@"title"]];
        self.labelDaiShouRenVlue.text = [CJControl getThreeUnaryValueStr:dicMuta4[@"content"]];
        
        // 货主成交单价
        NSMutableDictionary *dicMuta5 = arrContent[7 + index];
        self.labDanJia.text = [CJControl getThreeUnaryValueStr:dicMuta5[@"content"]];
        self.labDanJiaTitle.text = [CJControl getThreeUnaryValueStr:dicMuta5[@"title"]];
        
        // 司机承运单价
        NSMutableDictionary *dicMuta9 = arrContent[8 + index];
        self.labZhuangChe.text = [CJControl getThreeUnaryValueStr:dicMuta9[@"content"]];
        self.labZhuangCheTitle.text = [CJControl getThreeUnaryValueStr:dicMuta9[@"title"]];

        // 结算吨位
        NSMutableDictionary *dicMuta10 = arrContent[13 + index];
        self.labXieChe.text = [CJControl getThreeUnaryValueStr:dicMuta10[@"content"]];
        self.labXieCheTitle.text = [CJControl getThreeUnaryValueStr:dicMuta10[@"title"]];
        
        // 其他费用
        NSMutableDictionary *dicMuta13 = arrContent[15 + index];
        self.labOtherFeiYong.text = [CJControl getThreeUnaryValueStr:dicMuta13[@"content"]];

        // 异常费用
        NSMutableDictionary *dicMuta14 = arrContent[16 + index];
        self.labQiTaFeiYong.text = [CJControl getThreeUnaryValueStr:dicMuta14[@"content"]];

        //货主预付金额
        NSMutableDictionary *dicMuta15 = arrContent[18 + index];
        self.labYiChangFei.text = [CJControl getThreeUnaryValueStr:dicMuta15[@"content"]];

        // 货主保险费用
        NSMutableDictionary *dicMuta16 = arrContent[19 + index];
        self.labYuFuXianJin.text = [CJControl getThreeUnaryValueStr:dicMuta16[@"content"]];
        // 货主实付运费
        NSMutableDictionary *dicMuta18 = arrContent[20 + index];
        self.labYuFuYouKa.text = [CJControl getThreeUnaryValueStr:dicMuta18[@"content"]];

        // 货主实付现金
        NSMutableDictionary *dicMuta20 = arrContent[23 + index];
        self.labBaoXianFei.text = [CJControl getThreeUnaryValueStr:dicMuta20[@"content"]];
        
        
        //平台预付金额
        NSMutableDictionary *dicMuta21 = arrContent[25 + index];
        self.labShiJiJieSuan.text = [CJControl getThreeUnaryValueStr:dicMuta21[@"content"]];
        
        //司机合计
        NSMutableDictionary *dicMuta22 = arrContent[28 + index];
        self.labShiFuJin.text = [CJControl getThreeUnaryValueStr:dicMuta22[@"content"]];
        
        //司机实收现金
        NSMutableDictionary *dicMuta23 = arrContent[29 + index];
        self.fieldsSiJiShiSou.text = [CJControl getThreeUnaryValueStr:dicMuta23[@"content"]];
        



        [self.frameView layoutIfNeeded];
        self.frameView.frame = CGRectMake(0, self.height - self.frameView.height, kScreenWidth, self.frameView.height);
        [self addSubview:self.frameView];
        self.fieldsSiJiShiSou.delegate = self;
        [self setFieldLimit];
        
//    }
    return self;
}

- (void)setFieldLimit{
    self.fieldsSiJiShiSou.textFieldType = CJTextFieldStringTypeMoneyValue;
    self.fieldsSiJiShiSou.maxLength = 10;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    [UIView beginAnimations:@"Animation" context:nil];
    [UIView setAnimationDuration:0.20];
    //设置视图移动的位移
    self.frameView.frame = CGRectMake(0, 0, kScreenWidth, self.frameView.height);
    [UIView commitAnimations];
    
}
- (void)textFieldDidChangeSelection:(UITextField *)textField{
    DLog(@"====== text %@",textField.text);
    
    self.labShiFuJin.text = @([textField.text doubleValue] +  [self.labShiJiJieSuan.text doubleValue]).cj_money_description;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    [UIView beginAnimations:@"Animation" context:nil];
    [UIView setAnimationDuration:0.20];
    //设置视图移动的位移
    self.frameView.frame = CGRectMake(0, self.height - self.frameView.height, kScreenWidth, self.frameView.height);
    [UIView commitAnimations];
   
}


- (void)touchViewAccoutPriceAction{
  
    [self endEditing:NO];
    if ([CJRegular getIsStrEmpty:self.fieldsSiJiShiSou.text]) {
        [CJAlert showStringDismissWithDelay:@"请填写司机实收金额"];
        return;
    }
    
    if (self.sureBlock) {
        self.sureBlock(self.fieldsSiJiShiSou.text);
    }
    [self hidden];
}

- (void)touchViewCanclePriceAction{
    [self endEditing:NO];

    if (self.cancleBlock) {
        self.cancleBlock(@"取消了");
    }
    [self hidden];
}


/// 展示隐藏
- (void)show {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.center = [UIApplication sharedApplication].keyWindow.center;

    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
    }];
}

- (void)hidden {
    
    @weakify(self);
    [UIView animateWithDuration:0.5 animations:^{
        @strongify(self);
        self.alpha = 0.0;
    }];
}


@end
